package org.example.domain.activity_manage.service.Impl;

import org.example.domain.activity_manage.model.entity.ActivityStrategyPrizeEntity;
import org.example.domain.activity_manage.repository.IActivityStrategyPrizeRepo;
import org.example.domain.activity_manage.service.IActivityStrategyPrizeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lxc18
 * @date 2025/3/23 17:08
 * @description IActivityStrategyPrizeServiceImpl
 */
@Service
public class IActivityStrategyPrizeServiceImpl implements IActivityStrategyPrizeService {
    //开启事务
    @Resource
    private TransactionTemplate transactionTemplate;
    @Resource
    private IActivityStrategyPrizeRepo activityStrategyPrizeRepo;
    @Override
    public boolean updateStrategy(ActivityStrategyPrizeEntity activityStrategyPrize) {
        return activityStrategyPrizeRepo.updateStrategy(activityStrategyPrize);
    }

    @Override
    public ActivityStrategyPrizeEntity queryStrategy(Long activityId, String strategyName) {
        return activityStrategyPrizeRepo.queryStrategy(activityId, strategyName);
    }

    @Override
    public List<ActivityStrategyPrizeEntity> queryStrategyByActivityId(Long activityId) {
        return activityStrategyPrizeRepo.queryStrategyByActivityId(activityId);
    }


    @Override

    public boolean initDefaultCreateStrategy(Long activityId) {
        // 创建默认策略
        // 黑名单策略
        // 兜底策略
        // 事务成功返回 true
        // 事务回滚
        // 事务失败返回 false
        return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
            try {
                // 创建默认策略
                activityStrategyPrizeRepo.createStrategy(activityId, "default");
                // 黑名单策略
                activityStrategyPrizeRepo.createStrategy(activityId, "blacklist");
                // 兜底策略
                activityStrategyPrizeRepo.createStrategy(activityId, "guarantee");
                return true; // 事务成功返回 true
            } catch (Exception e) {
                status.setRollbackOnly(); // 事务回滚
                return false; // 事务失败返回 false
            }
        }));
    }
}
